package com.ruoyi.common.tio;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.SendInfo;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.http.common.HttpRequest;
import org.tio.http.common.HttpResponse;
import org.tio.http.common.HttpResponseStatus;
import org.tio.websocket.common.WsRequest;
import org.tio.websocket.common.WsResponse;
import org.tio.websocket.server.handler.IWsMsgHandler;

import java.io.IOException;

/**
 * websocket 处理函数
 * @author: suntf
 * @date: 2023/4/23 下午8:12
 */
@Slf4j
@Component
public class TioWsMsgHandler implements IWsMsgHandler {

    //public static TioConfig tioConfig;

    @Override
    public HttpResponse handshake(HttpRequest httpRequest, HttpResponse httpResponse, ChannelContext channelContext) {
        try {
            String userId = httpRequest.getParam("userId");
            if(StringUtils.isNotBlank(userId)) {
                //绑定用户
                Tio.bindUser(channelContext, userId);
                // 在线用户绑定到上下文 用于发送在线消息
                WsOnlineContext.bindUser(userId, channelContext);
            }
        } catch (Exception e) {
            e.printStackTrace();
            httpResponse.setStatus(HttpResponseStatus.getHttpStatus(401));
        }
        return httpResponse;
    }

    @Override
    public void onAfterHandshaked(HttpRequest httpRequest, HttpResponse httpResponse, ChannelContext channelContext) throws Exception {

    }

    @Override
    public Object onBytes(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) throws Exception {
        return null;
    }

    @Override
    public Object onClose(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) throws Exception {
        Tio.remove(channelContext, "receive close flag");
        return null;
    }

    /**
     * ws文本通信
     * @author: suntf
     * @date: 2023/4/24 上午8:49
     * @param wsRequest
     * @param text
     * @param channelContext
     * @return java.lang.Object
     */
    @Override
    public Object onText(WsRequest wsRequest, String text, ChannelContext channelContext) throws Exception {
        try {
            //System.out.println("text:"+text);
            ObjectMapper objectMapper = new ObjectMapper();
            SendInfo sendInfo = objectMapper.readValue(text, SendInfo.class);
            //心跳检测包
            if (Constants.MSG_PING.equals(sendInfo.getCode())) {
                WsResponse wsResponse = WsResponse.fromText(text, TioServerConfig.CHARSET);
                Tio.send(channelContext, wsResponse);
            } else if (Constants.MSG_MESSAGE.equals(sendInfo.getCode())) {

            }
            //准备就绪，需要发送离线消息
            else if (Constants.MSG_READY.equals(sendInfo.getCode())) {

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        //返回值是要发送给客户端的内容，一般都是返回null
        return null;
    }
}
